<?php

/**
 * 分享页面接口
 */

namespace app\h5\controller;

use function AlibabaCloud\Client\value;
use app\api\model\House as HouseModel;
use app\h5\model\House as House5Model;
use app\common\controller\H5Controller;
use app\common\lib\RespCode;
use think\Db;
use think\Exception;

class House extends H5Controller
{

    /**
     * 房源详情
     * @return false|string
     */
    public function getHouseInfo()
    {
        try {
            $params = $this->checkParams('key');
            if ($params === false) {
                return $this->resErr();
            }
            $params=$this->checkData($params);
            if (empty(Db::name('house')->getByid($params['id']))) {
                return $this->resErr('数据不存在', RespCode::DATA_ERR);
            }
//            halt($params);
            $h5Model = new House5Model();
            $agent = $h5Model->getAgentInfo($params['agent_id']);
            if (!$agent) {
                return $this->resErr();
            }
            $HouseModel = new HouseModel();
            $data = $HouseModel->get_house_detail($params, $this->headers);
            $info=$h5Model->getAgentHouseDesc($params['agent_id'],$params['id'],$data[0]);
            return $this->resData('success', ['house_info' => $info, 'history' => $data[1], 'success_history' => $data[2], 'nearData' => $data[3],'agent_info'=>$agent]);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    public function checkData($params){
        $key = base64_decode($params['key']);
        $key = substr($key, 16);
        $str = explode('@', $key);
        foreach ($str as $key => $value) {
            if (strpos($value, 'house_id')!== false) {
                $id = explode('=', $value)[1];
                $params['id'] = $id;
            }
            if (strpos($value, 'agent_id')!== false) {
                $agent_id = explode('=', $value)[1];
                $params['agent_id'] = $agent_id;
            }
        }
        return $params;
    }
}